package com.example.dllo.my36kr.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.model.NewsDatasBean;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 新闻界面的listView的适配器
 * Created by CPC_272 on 16/8/15.
 */
public class NewsAdapter extends BaseAdapter {
    private NewsDatasBean data;
    private Context context;

    public NewsAdapter(Context context) {
        this.context = context;
    }

    public void setData(NewsDatasBean data) {
        this.data = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data != null ? data.getData().getData1().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data.getData().getData1().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //把解析每层提出来  要用直接调取这个
        NewsDatasBean.DataBean.DataBean1 datas = data.getData().getData1().get(position);
        switch (datas.getColumnName()){//根据名字给NewsFragment ListView 里的这个这些名字设置颜色
            case "早期项目":
                holder.tv_tag.setTextColor(Color.GREEN);
                break;
            case "大公司":
                holder.tv_tag.setTextColor(Color.rgb(85,164,255));
                break;
            case "资本":
                holder.tv_tag.setTextColor(Color.BLUE);
                break;
            case "深度":
                holder.tv_tag.setTextColor(Color.RED);
                break;
            case "B轮后":
                holder.tv_tag.setTextColor(Color.rgb(98,197,245));
                break;
            case "研究":
                holder.tv_tag.setTextColor(Color.YELLOW);
                break;

        }

        holder.tv_tag.setText(datas.getColumnName());
        holder.tv_title.setText(datas.getTitle());
        holder.tv_writer.setText(datas.getUser().getName());
        Long time = datas.getPublishTime();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        String formatTime = format.format(new Date(time));
        holder.tv_time.setText(formatTime);
        Picasso.with(context).load(datas.getFeatureImg()).resize(70, 70).error(R.mipmap.common_loading).placeholder(R.mipmap.common_avatar).into(holder.img_news);
        return convertView;
    }

    class ViewHolder {
        private ImageView img_news;
        private TextView tv_title, tv_writer, tv_time, tv_tag;

        public ViewHolder(View view) {
            img_news = (ImageView) view.findViewById(R.id.img_ls_news);
            tv_title = (TextView) view.findViewById(R.id.tv_title_news);
            tv_writer = (TextView) view.findViewById(R.id.tv_writer_news);
            tv_time = (TextView) view.findViewById(R.id.tv_time_news);
            tv_tag = (TextView) view.findViewById(R.id.tv_tag_news);
        }
    }
}
